
package com.platform.modules.app.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.platform.annotation.IgnoreAuth;
import com.platform.common.utils.RestResponse;
import com.platform.common.utils.StringUtils;
import com.platform.modules.mall.entity.MallCategoryEntity;
import com.platform.modules.mall.service.MallCategoryService;
import com.platform.modules.mall.service.MallShopsCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author
 */
@Slf4j
@RestController
@RequestMapping("/app/category")
@Api(tags = "AppCategoryController|商品分类列表接口")
public class AppCategoryController extends AppBaseController {
    @Autowired
    private MallCategoryService categoryService;
    @Resource
    private MallShopsCategoryService shopsCategoryService;

//    /**
//     * 商品分类列表
//     */
//    @IgnoreAuth
//    @GetMapping("/shopsCategory")
//    @ApiOperation(value = "商品分类列表", notes = "商品分类列表")
//    @ApiImplicitParams({
//            @ApiImplicitParam(paramType = "query", name = "level", value = "分类等级", required = true, dataType = "string", example = "1")
//    })
//    public RestResponse shopsCategory(String level) {
//        //查询列表数据
//        List<MallCategoryEntity> data = categoryService.list(new QueryWrapper<MallCategoryEntity>().eq(StringUtils.isNotBlank(level), "LEVEL", level).eq("IS_SHOW", 1).orderByDesc("SORT"));
//
//        return RestResponse.success().put("data", data);
//    }

    /**
     * 商品分类列表
     */
    @IgnoreAuth
    @GetMapping("/categoryList")
    @ApiOperation(value = "商品分类列表", notes = "商品分类列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "level", value = "分类等级", required = true, dataType = "string", example = "1")
    })
    public RestResponse categoryList(String level) {
        //查询列表数据
        List<MallCategoryEntity> data = categoryService.list(new QueryWrapper<MallCategoryEntity>().eq(StringUtils.isNotBlank(level), "LEVEL", level).eq("IS_SHOW", 1).orderByDesc("SORT"));

        return RestResponse.success().put("data", data);
    }

    /**
     * 获取全部分类数据
     *
     * @return
     */
    @IgnoreAuth
    @GetMapping("shopsCategory")
    @ApiOperation(value = "查询店铺全部分类", notes = "根据分类ID获取分类下的所有子分类")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "商品分类ID", required = true, dataType = "int", example = "1")
    })
    public Object shopsCategory(@RequestParam Map<String,Object> params) {
        return RestResponse.success().put("data",  shopsCategoryService.getAllData(params));
    }

    /**
     * 获取全部分类数据
     *
     * @return
     */
    @IgnoreAuth
    @GetMapping("getAllData")
    @ApiOperation(value = "子商品分类", notes = "根据商品分类ID获取分类下的所有子分类")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "商品分类ID", required = true, dataType = "int", example = "1")
    })
    public Object getAllData(@RequestParam Map params) {
        return RestResponse.success().put("data",  categoryService.getAllData(params));
    }

    /**
     * 根据商品分类ID获取分类下的所有子分类
     *
     * @param id
     * @return
     */
    @IgnoreAuth
    @GetMapping("current")
    @ApiOperation(value = "子商品分类（原）", notes = "根据商品分类ID获取分类下的所有子分类")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "商品分类ID", required = true, dataType = "int", example = "1")
    })
    public Object current(String id) {
        MallCategoryEntity currentCategory = categoryService.getById(id);
        //获取子分类数据
        if (null != currentCategory) {
            Map<String, Object> params = new HashMap<>(4);
            params.put("isShow", 1);
            params.put("parentId", id);
            currentCategory.setSubCategoryList(categoryService.queryAll(params));
        }
        return RestResponse.success().put("data", currentCategory);
    }
}
